package cn.test.bookms.controller;

import cn.test.bookms.entity.MsBook;
import cn.test.bookms.entity.Student;
import cn.test.bookms.service.StudentService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.awt.print.Book;
import java.util.List;

/**
 * @_PackageName:cn.test.bookms.controller
 * @_ClassName:College
 * @_Description:学院
 * @_Author：笑老二
 * @_data 20/06/16 18:30
 */
@Controller
public class StudentController {

    @Autowired
    private StudentService service;


    /**
     * 工商学院
     *
     * @param model
     * @return
     */
    @RequestMapping("/toBusiness")
    public String business(Model model) {
        List<Student> students = service.findStuByCollege(7);
        System.out.println(students);
        model.addAttribute("students", students);
        return "/college/business";
    }

    /**
     * 计算机学院
     *
     * @param model
     * @return
     */
    @RequestMapping("/toComputer")
    public String computer(Model model) {
        List<Student> students = service.findStuByCollege(1);
        System.out.println(students);
        model.addAttribute("students", students);
        return "/college/computer";
    }

    /**
     * 机电学院
     *
     * @param model
     * @return
     */
    @RequestMapping("/toElectromechanical")
    public String electromechanical(Model model) {
        List<Student> students = service.findStuByCollege(5);
        System.out.println(students);
        model.addAttribute("students", students);
        return "/college/electromechanical";
    }

    /**
     * 纺织学院
     *
     * @param model
     * @return
     */
    @RequestMapping("/toSpinning")
    public String spinning(Model model) {
        List<Student> students = service.findStuByCollege(6);
        System.out.println(students);
        model.addAttribute("students", students);
        return "/college/spinning";
    }

    /**
     * 电信学院
     *
     * @param model
     * @return
     */
    @RequestMapping("/toTelecom")
    public String telecom(Model model) {
        List<Student> students = service.findStuByCollege(2);
        System.out.println(students);
        model.addAttribute("students", students);
        return "/college/telecom";
    }

    /**
     * 旅游学院
     *
     * @param model
     * @return
     */
    @RequestMapping("/toTravel")
    public String travel(Model model) {
        List<Student> students = service.findStuByCollege(4);
        System.out.println(students);
        model.addAttribute("students", students);
        return "/college/travel";
    }

    /**
     * 外贸学院
     *
     * @param model
     * @return
     */
    @RequestMapping("/toForeignTrade")
    public String foreignTrade(Model model) {
        List<Student> students = service.findStuByCollege(3);
        System.out.println(students);
        model.addAttribute("students", students);
        return "/college/foreignTrade";
    }

    /**
     * 展示学生信息列表,添加借书信息
     *
     * @param model
     * @param sid
     * @return
     */
    @RequestMapping("/stuLook")
    public String sutLook(Model model, Integer sid) {
        System.out.println(sid);
        Student student = service.findStuBySid(sid);
        model.addAttribute("student", student);
        return "/college/showStuMes";
    }

    /**
     * 借书信息
     *
     * @param model
     * @param college
     * @param sid
     * @param book_Id
     * @return
     */
    @RequestMapping("/borrowBook")
    public String borrowBook(Model model,
                             @RequestParam("s_id") String college,
                             @RequestParam("sid") Integer sid,
                             @RequestParam("bookId") Integer book_Id) {
        System.out.println("学生编号:" + sid);
        System.out.println("借书编号:" + book_Id);
        try {
            //如果没有这本书
            MsBook book =  service.findLibraryBook(book_Id);
        } catch (Exception e) {
            System.out.println("没有该编号");
            model.addAttribute("msg","没有该编号!请及时补充!");
            return "/college/showStuMes";
        }
        //判断当前的用户书籍是否被借
        MsBook book = service.findBookStatus(book_Id);
        if (book!=null){
            //没有借,更改状态
            int i  = service.updateBookStatus(book_Id);
            //增加学习借书信息
            service.addStuBorrowBook(sid,book_Id);
            if (i!=1){
                model.addAttribute("msg","编号出现异常,联系管理员");
                return "/college/showStuMes";
            }

        }else {
            model.addAttribute("msg","该图书已经借出,请更新数据库");
            return "/college/showStuMes";
        }

        if (college.contains("计算机")) {
            return "redirect:/toComputer";
        }
        if (college.contains("电信")) {
            return "redirect:/toTelecom";
        }
        if (college.contains("外贸")) {
            return "redirect:/toForeignTrade";
        }
        if (college.contains("旅游")) {
            return "redirect:/toTravel";
        }
        if (college.contains("机电")) {
            return "redirect:/toElectromechanical";
        }
        if (college.contains("纺织")) {
            return "redirect:/toSpinning";
        }

        return "redirect:/toBusiness";

    }

    /**
     * 归还图书
     * @param sid
     * @param book_id
     * @return
     */
    @RequestMapping("/delStuBorrow")
    public String delStuBorrow(
            @RequestParam("sid")Integer sid,
            @RequestParam("book_id")Integer book_id){
        System.out.println("学生编号:"+sid);
        System.out.println("借书编号:"+book_id);
        int i = service.delStuBorrow(sid,book_id);
        System.out.println("影响行数:"+i);
        return "redirect:/toComputer";
    }
}
